﻿using CastingWellEquipmentGUI.ViewModel.Common.Command;
using CastingWellEquipmentGUI.ViewModel.Common.WindowsManager;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CastingWellEquipmentGUI.ViewModel.Modal.CalendarWindow
{
    public class CalendarWindowViewModel : ViewModelBase
    {
        /// <summary>
        /// 设置小时的命令
        /// </summary>
        public DelegateCommand ShowSettingHourPanelCommand { set; get; }

        /// <summary>
        /// 设置分钟的命令
        /// </summary>
        public DelegateCommand SettingMinuteCommand { set; get; }

        /// <summary>
        /// 设置秒钟的命令
        /// </summary>
        public DelegateCommand SettingSecondCommand { set; get; }

        /// <summary>
        /// 显示设置分钟面板的命令
        /// </summary>
        public DelegateCommand ShowSettingMinutePanelCommand { set; get; }

        /// <summary>
        /// 显示设置秒面板的命令
        /// </summary>
        public DelegateCommand ShowSettingSecondPanelCommand { set; get; }

        /// <summary>
        /// 设置小时命令
        /// </summary>
        public DelegateCommand SettingHourCommand { set; get; }

        /// <summary>
        /// 关闭当前窗体命令
        /// </summary>
        public DelegateCommand CloseAppCommand { get; set; }
        /// <summary>
        /// 关闭并清除窗体命令
        /// </summary>
        public DelegateCommand ClearAndCloseAppCommand { get; set; }

        private DateTime _SelectedDate;
        public DateTime SelectedDate
        {
            get { return _SelectedDate; }
            set
            {
                _SelectedDate = value;
                ChangeDate(_SelectedDate);
                RaisePropertyChanged("SelectedDate");
            }
        }

        private string _Year;

        public string Year
        {
            get { return _Year; }
            set
            {
                _Year = value;
                RaisePropertyChanged("Year");
            }
        }


        private string _Month;

        public string Month
        {
            get { return _Month; }
            set
            {
                _Month = value;
                RaisePropertyChanged("Month");
            }
        }


        private string _Day;

        public string Day
        {
            get { return _Day; }
            set
            {
                _Day = value;
                RaisePropertyChanged("Day");
            }
        }


        private string _Hour;

        public string Hour
        {
            get { return _Hour; }
            set
            {
                _Hour = value;
                RaisePropertyChanged("Hour");
            }
        }



        private string _Minute;
        public string Minute
        {
            get { return _Minute; }
            set
            {
                _Minute = value;
                RaisePropertyChanged("Minute");
            }
        }

        private string _Second;
        public string Second
        {
            get { return _Second; }
            set
            {
                _Second = value;
                RaisePropertyChanged("Second");
            }
        }


        private string source;

        public CalendarWindowViewModel()
        {
            //初始化关闭命令
            CloseAppCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(CloseAppHandler)
            };
            ClearAndCloseAppCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(ClearAndCloseAppHandler)
            };
            SettingHourCommand = new DelegateCommand
            {
                ExecuteActionObj = new Action<object>(SettingHourHandler)
            };
            SettingMinuteCommand = new DelegateCommand
            {
                ExecuteActionObj = new Action<object>(SettingMinuteHandler)
            };

            SettingSecondCommand = new DelegateCommand
            {
                ExecuteActionObj = new Action<object>(SettingSecondHandler)
            };
            ShowSettingHourPanelCommand = new DelegateCommand
            {
                ExecuteActionObj = new Action<object>(ShowSettingHourPanelHandler)
            };


            ShowSettingMinutePanelCommand = new DelegateCommand
            {
                ExecuteActionObj = new Action<object>(ShowSettingMinutePanelHandler)
            };



            ShowSettingSecondPanelCommand = new DelegateCommand
            {
                ExecuteActionObj = new Action<object>(ShowSettingSecondPanelHandler)
            };


            SelectedDate = DateTime.Now;
            if (DateTime.Now.Hour < 10)
            {
                Hour = "0" + DateTime.Now.Hour.ToString();
            }
            else
            {
                Hour = DateTime.Now.Hour.ToString();
            }

            if (DateTime.Now.Minute < 10)
            {
                Minute = "0" + DateTime.Now.Minute.ToString();
            }
            else
            {
                Minute = DateTime.Now.Minute.ToString();
            }

            if (DateTime.Now.Second < 10)
            {
                Second = "0" + DateTime.Now.Second.ToString();
            }
            else
            {
                Second = DateTime.Now.Second.ToString();
            }

        }


        /// <summary>
        /// 退出程序命令处理程序
        /// </summary>
        /// <param name="obj">参数</param>
        private void CloseAppHandler(object obj)
        {
            string DateStr = Year + "-" + Month + "-" + Day + " " + Hour + ":" + Minute + ":" + Second;
            switch (source)
            {
                case "MSC_DateCreated_Start":
                    Messenger.Default.Send<string>(DateStr, "SetStartDate");
                    break;
                case "MSC_DateCreated_End":
                    Messenger.Default.Send<string>(DateStr, "SetEndDate");
                    break;
                case "StartTime":
                    Messenger.Default.Send<string>(DateStr, "SetStart");
                    break;
                case "EndTime":
                    Messenger.Default.Send<string>(DateStr, "SetEnd");
                    break;
                default:
                    break;
            }
            Messenger.Default.Send<NotificationMessage>(new NotificationMessage(this, "HideCalendarWindow"));
        }
        /// <summary>
        /// 关闭并清除窗体命令处理器
        /// </summary>
        /// <param name="obj"></param>
        private void ClearAndCloseAppHandler(object obj)
        {
            switch (source)
            {
                case "MSC_DateCreated_Start":
                    Messenger.Default.Send<string>("点击设置开始时间", "SetStartDate");
                    break;
                case "MSC_DateCreated_End":
                    Messenger.Default.Send<string>("点击设置结束时间", "SetEndDate");
                    break;
                default:
                    break;
            }
            Messenger.Default.Send<NotificationMessage>(new NotificationMessage(this, "HideCalendarWindow"));
        }

        /// <summary>
        /// 显示小时设值面板命令的处理方法
        /// </summary>
        /// <param name="obj"></param>
        private void ShowSettingHourPanelHandler(object obj)
        {
            WindowManager.ShowDialog("HourWindow", "");
        }

        /// <summary>
        /// 设置小时方法
        /// </summary>
        /// <param name="obj"></param>
        private void SettingHourHandler(object obj)
        {
            Hour = obj.ToString();
            Messenger.Default.Send<NotificationMessage>(new NotificationMessage(this, "HideHourWindow"));
        }
        /// <summary>
        /// 设置分钟方法
        /// </summary>
        /// <param name="obj"></param>
        private void SettingMinuteHandler(object obj)
        {
            Minute = obj.ToString();
            Messenger.Default.Send<NotificationMessage>(new NotificationMessage(this, "HideMinuteWindow"));
        }
        /// <summary>
        /// 设置秒钟方法
        /// </summary>
        /// <param name="obj"></param>
        private void SettingSecondHandler(object obj)
        {
            Second = obj.ToString();
            Messenger.Default.Send<NotificationMessage>(new NotificationMessage(this, "HideSecondWindow"));
        }
        /// <summary>
        /// 显示分钟设值面板命令的处理方法
        /// </summary>
        /// <param name="obj"></param>
        private void ShowSettingMinutePanelHandler(object obj)
        {
            WindowManager.ShowDialog("MinuteWindow", "");
        }
        /// <summary>
        /// 显示秒钟设值面板命令的处理方法
        /// </summary>
        /// <param name="obj"></param>
        private void ShowSettingSecondPanelHandler(object obj)
        {
            WindowManager.ShowDialog("SecondWindow", "");
        }

        public void SetSource(string param)
        {
            source = param;
        }

        private void ChangeDate(DateTime dateTime)
        {
            Year = dateTime.Year.ToString();
            if (dateTime.Month < 10)
            {
                Month = "0" + dateTime.Month.ToString();
            }
            else
            {
                Month = dateTime.Month.ToString();
            }
            if (dateTime.Day < 10)
            {
                Day = "0" + dateTime.Day.ToString();
            }
            else
            {
                Day = dateTime.Day.ToString();
            }
        }

    }
}
